package org.wattdepot.simulation.oahugrid.main.batch;

import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeFactory;

/**
 * Provides a helpful utility function for creating GregorianCalendar instances given a date string
 * like "2009-01-02".
 * 
 * @author Philip Johnson
 * 
 */
public class Tstamp {
  /**
   * Returns a GregorianCalendar, given its string representation, such as "2009-11-02". Missing
   * hours, minutes, second, millisecond, and timezone fields are given defaults.
   * 
   * @param rep The string representation.
   * @return The timestamp.
   * @throws Exception If the string cannot be parsed into a timestamp.
   */
  public static GregorianCalendar makeTimestamp(String rep) throws Exception {
    DatatypeFactory factory = DatatypeFactory.newInstance();
    long mills = factory.newXMLGregorianCalendar(rep).toGregorianCalendar().getTimeInMillis();
    return makeTimestamp(mills);
  }

  /**
   * Converts the specified time in milliseconds into a javax.xml.datatype.XMLGregorianCalendar.
   * 
   * @param timeInMillis the specified time in milliseconds to convert.
   * @return A new instance of a javax.xml.datatype.XmlGregorianCalendar
   */
  private static GregorianCalendar makeTimestamp(long timeInMillis) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTimeInMillis(timeInMillis);
    return calendar;
  }

}
